package com.cskaoyan.day7._07_stream;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Common-zhou
 * @since 2023-03-06 14:45
 */
public class Demo1 {
    public static void main(String[] args) {
        List<Student> studentList = new StudentList().studentList;

        //System.out.println(studentList);
        //System.out.println(studentList.size());

        //我们要对这个学生列表进行处理:
        //<span style="color:red">得到来自北京同学, 并且高度最高的三个同学</span>

        //test1();

        test2();


    }

    private static void test2() {
        List<Student> studentList = new StudentList().studentList;
        // 代码极度的简化
        // 但是可读性变差。
        // 如果别人用，你就用
        // 别人不用，你也不用。   ---》 公司写得过程中
        List<Student> students = studentList.stream()
            .filter(stu -> Student.Address.BJ.equals(stu.getAddress()))
            .sorted((o1, o2) -> o2.getHeight() - o1.getHeight()).limit(3)
            .collect(Collectors.toList());
        System.out.println(students);

    }

    private static void test1() {
        List<Student> studentList = new StudentList().studentList;
        //我们要对这个学生列表进行处理:
        //<span style="color:red">得到来自北京同学, 并且高度最高的三个同学</span>

        // 1.先筛选出北京的同学
        // 2.对这些数据排序

        // 1.遍历所有的学生,只取来自北京的同学

        List<Student> beijingStudent = new ArrayList<>();

        for (Student student : studentList) {
            if (Student.Address.BJ.equals(student.getAddress())) {
                //System.out.println(student);
                beijingStudent.add(student);
            }
            //if (student.getAddress().)
        }

        System.out.println(beijingStudent.size());
        System.out.println(beijingStudent);

        beijingStudent.sort(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o2.getHeight() - o1.getHeight();
            }
        });

        //System.out.println(beijingStudent);

        Student student1 = beijingStudent.remove(0);
        Student student2 = beijingStudent.remove(0);
        Student student3 = beijingStudent.remove(0);
        System.out.println(student1);
        System.out.println(student2);
        System.out.println(student3);


    }
}
